/*********************************************************************************
 *      Copyright:  (C) 2023 LN
 *                  All rights reserved.
 *
 *       Filename:  database.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2023年04月08日)
 *         Author:  Li Haipeng <NULL>
 *      ChangeLog:  1, Release initial version on "2023年04月08日 14时39分45秒"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <unistd.h>
#include <errno.h>
#include "logger.h"
#include "database.h"


static sqlite3     *db = NULL;

int open_db(char *tablename)
{
    char        *errmsg = NULL;
    int          rc;
    char         action[128];


	if( !tablename )
	{
		return 0;
	}

    rc = sqlite3_open("./database.db", &db);
    if( rc )
    {
        log_error("Can`t open database: %s", sqlite3_errmsg(db));	
    	return 0;
    }
    else
    {
        log_debug("Opened database successfully!");
    }

    memset(action, 0, sizeof(action));
    snprintf(action, sizeof(action),"create table if not exists %s ( date text NOT NULL, time float NOT NULL);", tablename);
	
    rc = sqlite3_exec(db, action, NULL, NULL, &errmsg);
    if( rc )
    {
        log_error("Can`t create table: %s", sqlite3_errmsg(db));
        return 0;
    }
    else
    {
        log_debug("open table");
	}

    return 1;
}

int insert_data(packet_t *pack)
{
    char        *errmsg = NULL;
    int          rc;
    char         action[128];


	if( !pack )
	{
		return 0;
	}

    memset(action, 0, sizeof(action));
    snprintf(action, sizeof(action),"insert into %s values ('%s', %f);", pack->devsn, pack->time, pack->temper);
	

    rc = sqlite3_exec(db, action, NULL, NULL, &errmsg);
    if( rc )
    {
        log_error("Can`t insert data: %s", sqlite3_errmsg(db));
		return 0;
    }
    else
    {
        log_debug("Insert data successfully!");
    }


    return 1;
}

int del_data(char *tablename)
{
    char        *errmsg = NULL;
    char         action[128];
    int          rc;


	if( !tablename )
	{
		return 0;
	}

    memset(action, 0, sizeof(action));
    snprintf(action, sizeof(action),"delete from %s where rowid = 1;", tablename);
	
    rc = sqlite3_exec(db, action, NULL, NULL, &errmsg);
    if( rc )
    {
        log_error("Can`t delete data: %s", sqlite3_errmsg(db));
		return 0;
    }
    else
    {
        log_debug("delete data successfully!");	
    }


    return 1;
}

int pop_data(packet_t *pack)
{
    char        *errmsg = NULL;
    char        **azResult = NULL;
    char         action[128];
    int          rc;
    int          i = 0;
    int	         nrow = 0;
    int	         ncolum = 0;


	if( !pack )
	{
		return 0;
	}

    memset(action, 0, sizeof(action));
    snprintf(action, sizeof(action),"select * from %s limit 1", pack->devsn);

    rc = sqlite3_get_table(db, action, &azResult, &nrow, &ncolum, &errmsg);
    if( rc == 0 )
    {
		if( (nrow*ncolum) > 1)
		{
			strcpy(pack->time, azResult[2]);
			pack->temper = atof(azResult[3]);
		}
		else 
		{
			return 0;
		}
    }
    else if(rc)
    {
        log_error("check table!:%s", errmsg);
        sqlite3_free(errmsg);
		return 0;
    }


    return 1;
}

int close_db()
{
	sqlite3_close(db);
}
